﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EnvDTE;
using EnvDTE80;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Resources;
using System.Globalization;
using System.Reflection;

namespace VsTortoiseSVN.Dialogs
{
    public partial class AboutDialog : Form
    {
        private const string UpdateCaption = "VsTortoise Update";

        /// <summary>
        /// Constructor
        /// </summary>
        public AboutDialog()
        {
            InitializeComponent();
            Text += Globals.VersionString;

            Webupdate.Instance.Completed += new RunWorkerCompletedEventHandler(WebupdateCompleted);

            string fileCheckString = string.Empty;
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.AppendLine();
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("VsTortoise Build " + Globals.VersionString + " (" + Assembly.GetExecutingAssembly().GetName().Version.ToString() + ")");

            if (!CheckFile("en-US\\VsTortoise.resources.dll", out fileCheckString))
                stringBuilder.AppendLine(fileCheckString);

            if(!CheckFile("SharpPlink-Win32.svnExe", out fileCheckString))
                stringBuilder.AppendLine(fileCheckString);

            if (!CheckFile("SharpSvn.dll", out fileCheckString))
                stringBuilder.AppendLine(fileCheckString);

            if(!CheckFile("SharpSvn-DB44-20-Win32.dll", out fileCheckString))
                stringBuilder.AppendLine(fileCheckString);

            if(!CheckFile("SharpSvn-SASL21-23-Win32.dll", out fileCheckString))
                stringBuilder.AppendLine(fileCheckString);

            textVersion.Text += stringBuilder.ToString();
        }

        bool CheckFile(string filename, out string text)
        {
            Assembly myAssembly = Assembly.GetExecutingAssembly();
            string folder = Path.GetDirectoryName(myAssembly.Location);

            if (File.Exists(Path.Combine(folder, filename)))
            {
                text = filename + "";
                return true;
            }
            else
            {
                text = filename + " missing";
                return false;
            }
        }

        private void WebupdateCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            buttonUpdate.Enabled = true;
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            Webupdate.Instance.Start(true);
            buttonUpdate.Enabled = false;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Win32.ShellExecute("http://www.console-dev.de", "");
        }

        private void linkLabelHomepage_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Win32.ShellExecute("http://www.console-dev.de/page_vstortoise/", "");
        }

    }
}
